package com.yanqu.road.server.cmd.kuang.cross;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.kuang.KuangConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.kuang.Kuang;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.kuang.KuangMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = GameProtocol.S_KUANG_CHOOSE_TREASURE_FROM_CROSS, desc = "选择宝库")
public class KuangChooseTreasureFromCrossCmd extends AbstractCommand {

    private Kuang.CrossKuangChooseTreasureRespMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = Kuang.CrossKuangChooseTreasureRespMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        KuangConfig config = KuangMgr.getConfig();
        Property property = config.getKuangSystemConfig().getLotteryCost();
        player.getModule(PreReduceModule.class).restorePreReduce(property);
        Kuang.KuangChooseTreasureRespMsg.Builder respMsg = Kuang.KuangChooseTreasureRespMsg.newBuilder();
        if (reqMsg.getRet() == 0 && reqMsg.hasDetail()) {
            // 扣道具
            player.getModule(CurrencyModule.class).removeCurrency(property, eLogMoneyType.Kuang, eLogMoneyType.KuangChooseTreasureCost);
            // 获得奖励
            player.getModule(CurrencyModule.class).addCurrency(PropertyHelper.parseStringToProperty(reqMsg.getDetail().getReward()), eLogMoneyType.Kuang, eLogMoneyType.KuangChooseTreasureGet);

            respMsg.setDetail(reqMsg.getDetail());
            respMsg.setTreasureData(reqMsg.getTreasureData());
        }
        respMsg.setRet(reqMsg.getRet());
        player.sendPacket(ClientProtocol.U_KUANG_CHOOSE_TREASURE, respMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            Kuang.KuangChooseTreasureRespMsg.Builder respMsg = Kuang.KuangChooseTreasureRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_KUANG_CHOOSE_TREASURE, respMsg);
        }
    }
}
